package com.newasia.management.controller;/**
 * Created by mic on 2019/8/1.
 */

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.IManagementCategoryService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.productCategory.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * lyh
 * 2019/10/24
 */
@Slf4j
@RestController
@RequestMapping("/management")
public class CategoryController {

    @Reference(timeout = 3000)
    IManagementCategoryService managementCategoryService;


    /**商品分类**/
    /**增*/
    @PostMapping("/itemCat")
    public ResponseData createItemCat(@RequestBody CreateItemCatRequest request, HttpServletRequest httpServletRequest){

        CreateItemCatResponse response= managementCategoryService.createItemCat(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删*/
    @DeleteMapping("/itemCat/{catId}")
    public ResponseData deleteItemCat(@PathVariable("catId") Long catId, HttpServletRequest httpServletRequest){

        DeleteItemCatResponse response= managementCategoryService.deleteItemCat(catId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改*/
    @PutMapping("/itemCat/{catId}")
    public ResponseData changeItemCat(@PathVariable("catId") Long catId, @RequestBody ChangeItemCatRequest request, HttpServletRequest httpServletRequest){

        ChangeItemCatResponse response= managementCategoryService.changeItemCat(catId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/itemCat")
    public ResponseData itemCat(@RequestParam(value="parentId",required=false) Long parentId,HttpServletRequest httpServletRequest){
        GetItemCatResponse response= managementCategoryService.getItemCat(parentId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }






}
